cmake_minimum_required(VERSION 3.15)
project(video_detection)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

option(USE_DUMMY_DETECTOR "Use dummy detector" OFF)
option(USE_YOLOV5S_DETECTOR "Use YOLOV5" OFF)
option(USE_YOLO11N_DETECTOR "Use Yolo11n" ON)

option(USE_HW_CODEC "use hardware accelerator" ON)
option(USE_TOPS_FFMPEG "use Tops ffmpeg" ON)
option(USE_TOPS_INFER "user tops infer" ON)


#set(CMAKE_CXX_FLAGS -fsanitize=address ${CMAKE_CXX_FLAGS})
add_subdirectory(otl)
add_subdirectory(video_tracker)

# Find required packages via shared modules
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/otl/common/cmake")
find_package(OpenCV REQUIRED)
find_package(Tops QUIET)
find_package(TopsFFmpeg REQUIRED)

if (USE_TOPS_INFER AND NOT USE_DUMMY_DETECTOR)
    find_package(TopsInference REQUIRED)
endif()

# Keep TOPSCV discovery as-is if needed
find_library(TOPSCV_LIBRARY topscv
        PATHS ${CMAKE_CURRENT_SOURCE_DIR}/topscv/lib
        DOC "TopsCV library")

if (NOT TOPSCV_LIBRARY)
    message(STATUS "[multi-streamer] TOPSCV library not found; continuing without it")
endif()

if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
    message(STATUS "detected macOS system")
    include_directories(/opt/homebrew/include)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
    include_directories($ENV{HOME}/buildroot /opt/tops/include)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    message(STATUS "detected  Windows system")
else()
	message(STATUS "Unknown：${CMAKE_SYSTEM_NAME}")
endif()

# FFmpeg is provided by Tops::FFMPEG imported target

# Remove manual link directories; imported targets propagate locations

if (USE_DUMMY_DETECTOR)
    set(DETECTOR_SRCS detector_dummy.cpp)
endif ()

if (USE_YOLOV5S_DETECTOR)
    set(DETECTOR_SRCS detector_yolov5.cpp detector_yolo.cpp utils.cpp)
endif()

if (USE_YOLO11N_DETECTOR)
    set(DETECTOR_SRCS detector_yolo11n.cpp detector_yolo.cpp utils.cpp)
endif()

message(STATUS "DETECTOR_SRC=${DETECTOR_SRCS}")
# Add source files
add_executable(${PROJECT_NAME}
        main.cpp
        streamer.cpp
        streamer.h
        detector.cpp
        detector.h
        device_manager.cpp
        device_manager.h
        ${DETECTOR_SRCS}
)

# Include directories (after target is created)
target_include_directories(${PROJECT_NAME}
        PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        ${CMAKE_CURRENT_SOURCE_DIR}/otl
        ${CMAKE_CURRENT_SOURCE_DIR}/video_tracker
        ${CMAKE_CURRENT_SOURCE_DIR}/topscv
        ${OpenCV_INCLUDE_DIRS}
        /usr/include/eigen3
)

# Link libraries
target_link_libraries(${PROJECT_NAME}
        PRIVATE
        video_tracker
        otl
        Tops::FFMPEG
        ${OpenCV_LIBS}
        pthread
)

if (USE_TOPS_INFER)
    target_link_libraries(${PROJECT_NAME} PRIVATE Tops::Inference Tops::RT)
endif()

if (TOPSCV_LIBRARY)
    target_link_libraries(${PROJECT_NAME} PRIVATE ${TOPSCV_LIBRARY})
endif()
